package com.zscg.sequence.ui;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.zgcs.sequence.net.in.JoinRoomPackage;
import com.zgcs.sequence.net.in.RoomListPackage;
import com.zgcs.sequence.tool.ServiceTool;
import com.zscg.sequence.R;
import com.zscg.sequence.data.Room;
import com.zscg.sequence.data.User;
import com.zscg.sequence.model.UserManager;
import com.zscg.sequence.service.SequenceService;
import com.zscg.sequence.ui.view.BaseListAdapter;
import com.zscg.sequence.ui.view.BaseListView;

public class GameActivity extends SequenceActivity {

	BaseListView gameList;

	ArrayList<Room> rooList;

	int defaultSize = 10;
	RoomListAdapter roomListAdapter;

	Room currentRoom;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);

		gameList = (BaseListView) findViewById(R.id.game_list);
		roomListAdapter = new RoomListAdapter(this);
		gameList.setAdapter(roomListAdapter);
		gameList.loadMore();

		gameList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				currentRoom = rooList.get(arg2);
				SequenceService.clinet.joinRoom(UserManager.getInstance()
						.getUser().getUserId(), currentRoom.roomId);
				showDialog(DIALOG_INROOM);

			}
		});

//		SequenceService.clinet
//				.roomList(UserManager.getInstance().getUser().getUserId(),
//						rooList == null ? 0 : rooList.size(), defaultSize);

	}

	@Override
	protected void addselfFilter() {
		intentFilter.addAction(ServiceTool.ORDER_RI);
		intentFilter.addAction(ServiceTool.ORDER_RGA);
	}

	@Override
	protected void disposeSelf(Context context, Intent intent) {
		String action = intent.getAction();
		if (ServiceTool.ORDER_RI.equals(action)) {

			RoomListPackage roomListPackage = (RoomListPackage) intent
					.getSerializableExtra(ServiceTool.ORDER_RI);
			if (roomListPackage.count > 0) {
				if (rooList == null)
					rooList = new ArrayList<Room>();
				rooList.addAll(roomListPackage.roomList);
				gameList.getLoadMoreView().ready();
				roomListAdapter.notifyDataSetChanged();
			} else {
				gameList.getLoadMoreView().nomore();
			}
		}
		if (ServiceTool.ORDER_RGA.equals(action)) {
			JoinRoomPackage joinRoom = (JoinRoomPackage) intent
					.getSerializableExtra(ServiceTool.ORDER_RGA);
			if (joinRoom != null
					&& joinRoom.getResult() == ServiceTool.RESULR_SUCESS) {
				User user = new User();
				user.setUserId(joinRoom.getUserID());
				user.setUserNickName(joinRoom.getUserNickname());
				currentRoom.state = joinRoom.getRoomState();

				if (user.getUserId() == UserManager.getInstance().getUser()
						.getUserId()) {
					user.setRoomOrder(true);
					dismissDialog(DIALOG_INROOM);
					Intent mintent = null;
					switch (joinRoom.getRoomState()) {
					case JoinRoomPackage.STATE_ROOM_READY:
						mintent = new Intent(GameActivity.this,
								GameReadyActivity.class);
						break;
					case JoinRoomPackage.STATE_ROOM_GAMING:
					case JoinRoomPackage.STATE_ROOM_OVER_ONCE:
						mintent = new Intent(GameActivity.this,
								GamingActivity.class);
						break;
					}
					mintent.putExtra("room", currentRoom);
					startActivity(mintent);

				} else {

				}
			}

		}

	}

	@Override
	protected void disposeLogin(Intent intent) {
		System.out.println("---------------------2");

	}

	class RoomListAdapter extends BaseListAdapter {

		public RoomListAdapter(Context context) {
			super(context);
			rooList = new ArrayList<Room>();
		}

		@Override
		public int getCount() {
			return rooList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View inflateItemView(int position, ViewGroup parent) {
			return mInflater.inflate(R.layout.game_list_item, null);
		}

		@Override
		public Object getItemViewHolder(View convertView) {
			ViewHolder holder = new ViewHolder();
			holder.itemFirst = (TextView) convertView
					.findViewById(R.id.item_first);
			holder.itemSquence = (TextView) convertView
					.findViewById(R.id.item_sequence);
			holder.itemDesc = (TextView) convertView
					.findViewById(R.id.item_desc);
			holder.itemPlayNum = (TextView) convertView
					.findViewById(R.id.item_number);

			return holder;
		}

		@Override
		public void fillViewHolder(int position, Object viewHolder,
				ViewGroup parent) {
			ViewHolder holder = (ViewHolder) viewHolder;
			Room room = rooList.get(position);
			holder.itemFirst.setText(room.keywords);
			holder.itemSquence.setText(room.title);
			holder.itemDesc.setText(room.desc);
			holder.itemPlayNum.setText(room.personNum + "人在玩");

		}

		@Override
		protected void loadMore() {

			SequenceService.clinet.roomList(UserManager.getInstance().getUser()
					.getUserId(), rooList.size(), defaultSize);
		}

	}

	class ViewHolder {
		TextView itemFirst;
		TextView itemSquence;
		TextView itemDesc;
		TextView itemPlayNum;
	}

	public static final int DIALOG_INROOM = 1;

	protected Dialog onCreateDialog(int id) {
		ProgressDialog dialog;
		String msg = null;
		switch (id) {
		case DIALOG_INROOM:
			msg = "正在进入房间";
			break;
		}
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(false);
		dialog.setMessage(msg);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setCancelable(true);
		dialog.show();
		return dialog;
	}
}
